package com.hyb.algorithm.data.struct.dp;

/**
 * 91. 解码方法
 * https://leetcode.cn/problems/decode-ways/
 *
 * @author: ybhu
 * @create: 2024/01/04 17:39
 */
public class NumDecodings {

    public static void main(String[] args) {

        int ret = new NumDecodings().numDecodings("226");

        System.out.println(ret);

    }

    public int numDecodings(String s) {

        int n = s.length();
        int[] memo = new int[n+1];

        memo[0] = 1;
        for (int i = 1; i <= n; i++) {
            if (s.charAt(i - 1) != '0') {
                memo[i] = memo[i] + memo[i - 1];
            }

            if (i>=2&&s.charAt(i - 2) != '0') {
                int val = (s.charAt(i - 2) - '0') * 10 + (s.charAt(i-1)-'0');
                if (val <= 26) {
                    memo[i] = memo[i] + memo[i - 2];
                }
            }

        }


        return memo[n ];

    }
}
